package persistence.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import persistence.db.DataSet;
import persistence.db.DatabaseConnector;
import persistence.model.AbstractModel;
import persistence.model.SEE;
import persistence.model.Service;

public class SEEDAO extends AbstractDAO{
	
	private SEE see;
	
	public SEEDAO() {
		setTableName("SEE");
	}

	public SEEDAO(SEE see) {
		this();
		this.see = see;
	}
	
	public DataSet all() {
		return new DataSet(this);
	}

	public AbstractModel makeModel(ResultSet rset) {
		try {
			
			setId(rset.getInt(1));
			DataSet serviceSet = Service.all().filter("FK_SEE = " + getId());
			
			return new SEE(this.getId(),
					rset.getInt("nbPeriodes"),
					rset.getInt("fk_see_matiere"),
					rset.getInt("fk_see_type_enseignement"),
					serviceSet);
		} catch (SQLException e) {
			System.out.println("makeModel error");
			e.printStackTrace();
		}
		return null;
	}

	public void save() {
		String query;
		ResultSet result;
		query = "INSERT INTO " + getTableName() + " VALUES ( S_PK_" + getTableName() + ".nextval, " +
		see.getNombreDePeriodes() + ", " +
		see.getIdMatiere() +", "+
		see.getIdTypeEnseignement() + ")";
		/* Execute la requete */
		result = DatabaseConnector.getInstance().execute(query);
		setId(this.getId()); /* Mise a jour de l'identifiant */
	}

	public void save(int id) {
		String query;
		ResultSet result;
		query = "UPDATE " + getTableName() +
			" SET nbPeriodes = " + see.getNombreDePeriodes() +
			", fk_see_matiere = " + see.getIdMatiere() +
			", fk_see_type_enseignement = "+ see.getIdTypeEnseignement() +
			" WHERE ID_"+ getTableName() + " = "+ id;
		/* Execute la requete */
		result = DatabaseConnector.getInstance().execute(query);
	}

}
